﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLNS.Models;
namespace QLNS.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        public JsonResult GetPagingGrid(int pagenum, int pagesize, string sortdatafield, string sortorder, int id = 0)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            var dbResult = (from dt in context.DSNguoiDung
                            // where dt.IDCanBo == id
                            select dt).ToList();
            //var dbResult = db.Customers.ToList();
            var customers = (from customer in dbResult
                             select new
                             {
                                 customer.ID,
                                 customer.TaiKhoan,
                                 customer.MatKhau,
                                 customer.HoTen,
                                 //customer.Quyen,
                                 customer.GhiChu
                             });
            if (sortorder != null)
            {
                if (sortorder == "asc")
                {
                    customers = customers.OrderBy(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
                else if (sortorder == "desc")
                {
                    customers = customers.OrderByDescending(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
            }
            customers = customers.Skip(pagenum * pagesize).Take(pagesize);
            var result = new
            {
                TotalRows = dbResult.Count,
                Rows = customers
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult User()
        {
            
            return View();
        }
        [HttpPost]
        public ActionResult Index(FormCollection form)
        {

            NguoiDung tk = new NguoiDung();
            tk.TaiKhoan = form["TaiKhoan"];
            tk.MatKhau = form["MatKhau"];
            NguoiDung kt = NguoiDungModels.DangNhap(tk);
            if (kt.Quyen > 0)
            {
                Session["TaiKhoan"] = kt.TaiKhoan;
                Session["Quyen"] = kt.Quyen;
                    return RedirectToAction("Index", "Home");
            }
                else
                {
                    return RedirectToAction("Index", "Login");
                }
            
         }        
        public ActionResult Logout()
        {
            Session["TaiKhoan"] = null;
            Session["Quyen"] = null;
            return RedirectToAction("Index", "Login");
        }
        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Add(FormCollection fm)
        {
            
            NguoiDung ob = new NguoiDung();
            //gan vao bang
            ob.TaiKhoan = fm["TaiKhoan"];
            ob.MatKhau = fm["MatKhau"];
            ob.HoTen = fm["HoTen1"];
            ob.Quyen = Convert.ToInt32(fm["Quyen"]);
            ob.GhiChu = fm["GhiChu"];
            NguoiDungModels.Add(ob);

            return RedirectToAction("User", "Login");
        }
       

        public ActionResult Delete(int id)
        {

            NguoiDungModels.Delete(id);
            return RedirectToAction("User", "Login");
        }

        
        public ActionResult Update(int id)
        {
            NguoiDung listDT = NguoiDungModels.GetChiTietNguoiDung(id);
            ViewBag.DSNguoiDung = listDT;
            return View();
        }
        [HttpPost]
        public ActionResult Update(FormCollection fm, int id)
        {
            NguoiDung ob = NguoiDungModels.GetChiTietNguoiDung(id);
            ob.TaiKhoan = fm["TaiKhoan"];            
            ob.MatKhau = fm["MatKhau"];           
            ob.HoTen = fm["HoTen1"];            
            ob.Quyen = Convert.ToInt32(fm["Quyen"]);
            ob.GhiChu = fm["GhiChu"];
           NguoiDungModels.Update(ob);
           return RedirectToAction("User", "Login");
        }

       
    }
}
